{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-10-27T09:35:51.330643Z",
     "start_time": "2025-10-27T09:35:51.325834Z"
    }
   },
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import pymysql\n",
    "import sqlalchemy\n",
    "from sqlalchemy import create_engine\n",
    "from mysql import connector"
   ],
   "outputs": [],
   "execution_count": 69
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-27T09:35:51.355787Z",
     "start_time": "2025-10-27T09:35:51.348475Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(sqlalchemy.__version__)\n",
    "print(pymysql.__version__)\n",
    "pymysql.install_as_MySQLdb()\n"
   ],
   "id": "4f94fa733e8dbc55",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2.0.39\n",
      "1.4.6\n"
     ]
    }
   ],
   "execution_count": 70
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-27T09:35:51.396530Z",
     "start_time": "2025-10-27T09:35:51.382755Z"
    }
   },
   "cell_type": "code",
   "source": [
    "df = pd.DataFrame({\n",
    "    \"班级\":[\"一年级\",\"二年级\",\"三年级\",\"四年级\"],\n",
    "    \"男生人数\":[25,23,27,30],\n",
    "    \"女生人数\":[19,17,20,20]\n",
    "})\n",
    "df"
   ],
   "id": "1fee913e0f08a6eb",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "    班级  男生人数  女生人数\n",
       "0  一年级    25    19\n",
       "1  二年级    23    17\n",
       "2  三年级    27    20\n",
       "3  四年级    30    20"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>班级</th>\n",
       "      <th>男生人数</th>\n",
       "      <th>女生人数</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>一年级</td>\n",
       "      <td>25</td>\n",
       "      <td>19</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>二年级</td>\n",
       "      <td>23</td>\n",
       "      <td>17</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>三年级</td>\n",
       "      <td>27</td>\n",
       "      <td>20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>四年级</td>\n",
       "      <td>30</td>\n",
       "      <td>20</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 71
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-27T09:35:51.535061Z",
     "start_time": "2025-10-27T09:35:51.440025Z"
    }
   },
   "cell_type": "code",
   "source": [
    "engine = create_engine('mysql+mysqlconnector://root:asdf123!@127.0.0.1:3306/students?charset=utf8')\n",
    "df.to_sql('students', con = engine, if_exists='replace',index=False)\n"
   ],
   "id": "39bce35004c30673",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 72
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-27T09:36:20.638152Z",
     "start_time": "2025-10-27T09:36:20.380428Z"
    }
   },
   "cell_type": "code",
   "source": [
    "sql = 'select * from person_info where id>3;'\n",
    "df = pd.read_sql(sql, engine)\n",
    "df"
   ],
   "id": "60fda3726b930aec",
   "outputs": [
    {
     "ename": "ProgrammingError",
     "evalue": "(mysql.connector.errors.ProgrammingError) 1146 (42S02): Table 'students.person_info' doesn't exist\n[SQL: select * from person_info where id>3;]\n(Background on this error at: https://sqlalche.me/e/20/f405)",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mMySQLInterfaceError\u001B[0m                       Traceback (most recent call last)",
      "File \u001B[1;32mG:\\anaconda\\Lib\\site-packages\\mysql\\connector\\connection_cext.py:772\u001B[0m, in \u001B[0;36mCMySQLConnection.cmd_query\u001B[1;34m(self, query, raw, buffered, raw_as_string, **kwargs)\u001B[0m\n\u001B[0;32m    770\u001B[0m     \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39m_local_infile_filenames \u001B[38;5;241m=\u001B[39m \u001B[38;5;28;01mNone\u001B[39;00m\n\u001B[1;32m--> 772\u001B[0m     \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39m_cmysql\u001B[38;5;241m.\u001B[39mquery(\n\u001B[0;32m    773\u001B[0m         query,\n\u001B[0;32m    774\u001B[0m         raw\u001B[38;5;241m=\u001B[39mraw,\n\u001B[0;32m    775\u001B[0m         buffered\u001B[38;5;241m=\u001B[39mbuffered,\n\u001B[0;32m    776\u001B[0m         raw_as_string\u001B[38;5;241m=\u001B[39mraw_as_string,\n\u001B[0;32m    777\u001B[0m         query_attrs\u001B[38;5;241m=\u001B[39m\u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mquery_attrs,\n\u001B[0;32m    778\u001B[0m     )\n\u001B[0;32m    779\u001B[0m \u001B[38;5;28;01mexcept\u001B[39;00m MySQLInterfaceError \u001B[38;5;28;01mas\u001B[39;00m err:\n",
      "\u001B[1;31mMySQLInterfaceError\u001B[0m: Table 'students.person_info' doesn't exist",
      "\nThe above exception was the direct cause of the following exception:\n",
      "\u001B[1;31mProgrammingError\u001B[0m                          Traceback (most recent call last)",
      "File \u001B[1;32mG:\\anaconda\\Lib\\site-packages\\sqlalchemy\\engine\\base.py:1964\u001B[0m, in \u001B[0;36mConnection._exec_single_context\u001B[1;34m(self, dialect, context, statement, parameters)\u001B[0m\n\u001B[0;32m   1963\u001B[0m     \u001B[38;5;28;01mif\u001B[39;00m \u001B[38;5;129;01mnot\u001B[39;00m evt_handled:\n\u001B[1;32m-> 1964\u001B[0m         \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mdialect\u001B[38;5;241m.\u001B[39mdo_execute(\n\u001B[0;32m   1965\u001B[0m             cursor, str_statement, effective_parameters, context\n\u001B[0;32m   1966\u001B[0m         )\n\u001B[0;32m   1968\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39m_has_events \u001B[38;5;129;01mor\u001B[39;00m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mengine\u001B[38;5;241m.\u001B[39m_has_events:\n",
      "File \u001B[1;32mG:\\anaconda\\Lib\\site-packages\\sqlalchemy\\engine\\default.py:942\u001B[0m, in \u001B[0;36mDefaultDialect.do_execute\u001B[1;34m(self, cursor, statement, parameters, context)\u001B[0m\n\u001B[0;32m    941\u001B[0m \u001B[38;5;28;01mdef\u001B[39;00m\u001B[38;5;250m \u001B[39m\u001B[38;5;21mdo_execute\u001B[39m(\u001B[38;5;28mself\u001B[39m, cursor, statement, parameters, context\u001B[38;5;241m=\u001B[39m\u001B[38;5;28;01mNone\u001B[39;00m):\n\u001B[1;32m--> 942\u001B[0m     cursor\u001B[38;5;241m.\u001B[39mexecute(statement, parameters)\n",
      "File \u001B[1;32mG:\\anaconda\\Lib\\site-packages\\mysql\\connector\\cursor_cext.py:353\u001B[0m, in \u001B[0;36mCMySQLCursor.execute\u001B[1;34m(self, operation, params, map_results)\u001B[0m\n\u001B[0;32m    351\u001B[0m \u001B[38;5;28;01mtry\u001B[39;00m:\n\u001B[0;32m    352\u001B[0m     \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39m_handle_result(\n\u001B[1;32m--> 353\u001B[0m         \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39m_connection\u001B[38;5;241m.\u001B[39mcmd_query(\n\u001B[0;32m    354\u001B[0m             \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39m_stmt_partition[\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mmappable_stmt\u001B[39m\u001B[38;5;124m\"\u001B[39m],\n\u001B[0;32m    355\u001B[0m             raw\u001B[38;5;241m=\u001B[39m\u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39m_raw,\n\u001B[0;32m    356\u001B[0m             buffered\u001B[38;5;241m=\u001B[39m\u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39m_buffered,\n\u001B[0;32m    357\u001B[0m             raw_as_string\u001B[38;5;241m=\u001B[39m\u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39m_raw_as_string,\n\u001B[0;32m    358\u001B[0m         )\n\u001B[0;32m    359\u001B[0m     )\n\u001B[0;32m    360\u001B[0m \u001B[38;5;28;01mexcept\u001B[39;00m MySQLInterfaceError \u001B[38;5;28;01mas\u001B[39;00m err:\n",
      "File \u001B[1;32mG:\\anaconda\\Lib\\site-packages\\mysql\\connector\\opentelemetry\\context_propagation.py:97\u001B[0m, in \u001B[0;36mwith_context_propagation.<locals>.wrapper\u001B[1;34m(cnx, *args, **kwargs)\u001B[0m\n\u001B[0;32m     96\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m \u001B[38;5;129;01mnot\u001B[39;00m OTEL_ENABLED \u001B[38;5;129;01mor\u001B[39;00m \u001B[38;5;129;01mnot\u001B[39;00m cnx\u001B[38;5;241m.\u001B[39motel_context_propagation:\n\u001B[1;32m---> 97\u001B[0m     \u001B[38;5;28;01mreturn\u001B[39;00m method(cnx, \u001B[38;5;241m*\u001B[39margs, \u001B[38;5;241m*\u001B[39m\u001B[38;5;241m*\u001B[39mkwargs)\n\u001B[0;32m     99\u001B[0m current_span \u001B[38;5;241m=\u001B[39m trace\u001B[38;5;241m.\u001B[39mget_current_span()\n",
      "File \u001B[1;32mG:\\anaconda\\Lib\\site-packages\\mysql\\connector\\connection_cext.py:781\u001B[0m, in \u001B[0;36mCMySQLConnection.cmd_query\u001B[1;34m(self, query, raw, buffered, raw_as_string, **kwargs)\u001B[0m\n\u001B[0;32m    780\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m \u001B[38;5;28mhasattr\u001B[39m(err, \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124merrno\u001B[39m\u001B[38;5;124m\"\u001B[39m):\n\u001B[1;32m--> 781\u001B[0m     \u001B[38;5;28;01mraise\u001B[39;00m get_mysql_exception(\n\u001B[0;32m    782\u001B[0m         err\u001B[38;5;241m.\u001B[39merrno, msg\u001B[38;5;241m=\u001B[39merr\u001B[38;5;241m.\u001B[39mmsg, sqlstate\u001B[38;5;241m=\u001B[39merr\u001B[38;5;241m.\u001B[39msqlstate\n\u001B[0;32m    783\u001B[0m     ) \u001B[38;5;28;01mfrom\u001B[39;00m\u001B[38;5;250m \u001B[39m\u001B[38;5;21;01merr\u001B[39;00m\n\u001B[0;32m    784\u001B[0m \u001B[38;5;28;01mraise\u001B[39;00m InterfaceError(\u001B[38;5;28mstr\u001B[39m(err)) \u001B[38;5;28;01mfrom\u001B[39;00m\u001B[38;5;250m \u001B[39m\u001B[38;5;21;01merr\u001B[39;00m\n",
      "\u001B[1;31mProgrammingError\u001B[0m: 1146 (42S02): Table 'students.person_info' doesn't exist",
      "\nThe above exception was the direct cause of the following exception:\n",
      "\u001B[1;31mProgrammingError\u001B[0m                          Traceback (most recent call last)",
      "Cell \u001B[1;32mIn[74], line 2\u001B[0m\n\u001B[0;32m      1\u001B[0m sql \u001B[38;5;241m=\u001B[39m \u001B[38;5;124m'\u001B[39m\u001B[38;5;124mselect * from person_info where id>3;\u001B[39m\u001B[38;5;124m'\u001B[39m\n\u001B[1;32m----> 2\u001B[0m df \u001B[38;5;241m=\u001B[39m pd\u001B[38;5;241m.\u001B[39mread_sql(sql, engine)\n\u001B[0;32m      3\u001B[0m df\n",
      "File \u001B[1;32mG:\\anaconda\\Lib\\site-packages\\pandas\\io\\sql.py:734\u001B[0m, in \u001B[0;36mread_sql\u001B[1;34m(sql, con, index_col, coerce_float, params, parse_dates, columns, chunksize, dtype_backend, dtype)\u001B[0m\n\u001B[0;32m    724\u001B[0m     \u001B[38;5;28;01mreturn\u001B[39;00m pandas_sql\u001B[38;5;241m.\u001B[39mread_table(\n\u001B[0;32m    725\u001B[0m         sql,\n\u001B[0;32m    726\u001B[0m         index_col\u001B[38;5;241m=\u001B[39mindex_col,\n\u001B[1;32m   (...)\u001B[0m\n\u001B[0;32m    731\u001B[0m         dtype_backend\u001B[38;5;241m=\u001B[39mdtype_backend,\n\u001B[0;32m    732\u001B[0m     )\n\u001B[0;32m    733\u001B[0m \u001B[38;5;28;01melse\u001B[39;00m:\n\u001B[1;32m--> 734\u001B[0m     \u001B[38;5;28;01mreturn\u001B[39;00m pandas_sql\u001B[38;5;241m.\u001B[39mread_query(\n\u001B[0;32m    735\u001B[0m         sql,\n\u001B[0;32m    736\u001B[0m         index_col\u001B[38;5;241m=\u001B[39mindex_col,\n\u001B[0;32m    737\u001B[0m         params\u001B[38;5;241m=\u001B[39mparams,\n\u001B[0;32m    738\u001B[0m         coerce_float\u001B[38;5;241m=\u001B[39mcoerce_float,\n\u001B[0;32m    739\u001B[0m         parse_dates\u001B[38;5;241m=\u001B[39mparse_dates,\n\u001B[0;32m    740\u001B[0m         chunksize\u001B[38;5;241m=\u001B[39mchunksize,\n\u001B[0;32m    741\u001B[0m         dtype_backend\u001B[38;5;241m=\u001B[39mdtype_backend,\n\u001B[0;32m    742\u001B[0m         dtype\u001B[38;5;241m=\u001B[39mdtype,\n\u001B[0;32m    743\u001B[0m     )\n",
      "File \u001B[1;32mG:\\anaconda\\Lib\\site-packages\\pandas\\io\\sql.py:1836\u001B[0m, in \u001B[0;36mSQLDatabase.read_query\u001B[1;34m(self, sql, index_col, coerce_float, parse_dates, params, chunksize, dtype, dtype_backend)\u001B[0m\n\u001B[0;32m   1779\u001B[0m \u001B[38;5;28;01mdef\u001B[39;00m\u001B[38;5;250m \u001B[39m\u001B[38;5;21mread_query\u001B[39m(\n\u001B[0;32m   1780\u001B[0m     \u001B[38;5;28mself\u001B[39m,\n\u001B[0;32m   1781\u001B[0m     sql: \u001B[38;5;28mstr\u001B[39m,\n\u001B[1;32m   (...)\u001B[0m\n\u001B[0;32m   1788\u001B[0m     dtype_backend: DtypeBackend \u001B[38;5;241m|\u001B[39m Literal[\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mnumpy\u001B[39m\u001B[38;5;124m\"\u001B[39m] \u001B[38;5;241m=\u001B[39m \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mnumpy\u001B[39m\u001B[38;5;124m\"\u001B[39m,\n\u001B[0;32m   1789\u001B[0m ) \u001B[38;5;241m-\u001B[39m\u001B[38;5;241m>\u001B[39m DataFrame \u001B[38;5;241m|\u001B[39m Iterator[DataFrame]:\n\u001B[0;32m   1790\u001B[0m \u001B[38;5;250m    \u001B[39m\u001B[38;5;124;03m\"\"\"\u001B[39;00m\n\u001B[0;32m   1791\u001B[0m \u001B[38;5;124;03m    Read SQL query into a DataFrame.\u001B[39;00m\n\u001B[0;32m   1792\u001B[0m \n\u001B[1;32m   (...)\u001B[0m\n\u001B[0;32m   1834\u001B[0m \n\u001B[0;32m   1835\u001B[0m \u001B[38;5;124;03m    \"\"\"\u001B[39;00m\n\u001B[1;32m-> 1836\u001B[0m     result \u001B[38;5;241m=\u001B[39m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mexecute(sql, params)\n\u001B[0;32m   1837\u001B[0m     columns \u001B[38;5;241m=\u001B[39m result\u001B[38;5;241m.\u001B[39mkeys()\n\u001B[0;32m   1839\u001B[0m     \u001B[38;5;28;01mif\u001B[39;00m chunksize \u001B[38;5;129;01mis\u001B[39;00m \u001B[38;5;129;01mnot\u001B[39;00m \u001B[38;5;28;01mNone\u001B[39;00m:\n",
      "File \u001B[1;32mG:\\anaconda\\Lib\\site-packages\\pandas\\io\\sql.py:1659\u001B[0m, in \u001B[0;36mSQLDatabase.execute\u001B[1;34m(self, sql, params)\u001B[0m\n\u001B[0;32m   1657\u001B[0m args \u001B[38;5;241m=\u001B[39m [] \u001B[38;5;28;01mif\u001B[39;00m params \u001B[38;5;129;01mis\u001B[39;00m \u001B[38;5;28;01mNone\u001B[39;00m \u001B[38;5;28;01melse\u001B[39;00m [params]\n\u001B[0;32m   1658\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m \u001B[38;5;28misinstance\u001B[39m(sql, \u001B[38;5;28mstr\u001B[39m):\n\u001B[1;32m-> 1659\u001B[0m     \u001B[38;5;28;01mreturn\u001B[39;00m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mcon\u001B[38;5;241m.\u001B[39mexec_driver_sql(sql, \u001B[38;5;241m*\u001B[39margs)\n\u001B[0;32m   1660\u001B[0m \u001B[38;5;28;01mreturn\u001B[39;00m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mcon\u001B[38;5;241m.\u001B[39mexecute(sql, \u001B[38;5;241m*\u001B[39margs)\n",
      "File \u001B[1;32mG:\\anaconda\\Lib\\site-packages\\sqlalchemy\\engine\\base.py:1776\u001B[0m, in \u001B[0;36mConnection.exec_driver_sql\u001B[1;34m(self, statement, parameters, execution_options)\u001B[0m\n\u001B[0;32m   1771\u001B[0m execution_options \u001B[38;5;241m=\u001B[39m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39m_execution_options\u001B[38;5;241m.\u001B[39mmerge_with(\n\u001B[0;32m   1772\u001B[0m     execution_options\n\u001B[0;32m   1773\u001B[0m )\n\u001B[0;32m   1775\u001B[0m dialect \u001B[38;5;241m=\u001B[39m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mdialect\n\u001B[1;32m-> 1776\u001B[0m ret \u001B[38;5;241m=\u001B[39m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39m_execute_context(\n\u001B[0;32m   1777\u001B[0m     dialect,\n\u001B[0;32m   1778\u001B[0m     dialect\u001B[38;5;241m.\u001B[39mexecution_ctx_cls\u001B[38;5;241m.\u001B[39m_init_statement,\n\u001B[0;32m   1779\u001B[0m     statement,\n\u001B[0;32m   1780\u001B[0m     \u001B[38;5;28;01mNone\u001B[39;00m,\n\u001B[0;32m   1781\u001B[0m     execution_options,\n\u001B[0;32m   1782\u001B[0m     statement,\n\u001B[0;32m   1783\u001B[0m     distilled_parameters,\n\u001B[0;32m   1784\u001B[0m )\n\u001B[0;32m   1786\u001B[0m \u001B[38;5;28;01mreturn\u001B[39;00m ret\n",
      "File \u001B[1;32mG:\\anaconda\\Lib\\site-packages\\sqlalchemy\\engine\\base.py:1843\u001B[0m, in \u001B[0;36mConnection._execute_context\u001B[1;34m(self, dialect, constructor, statement, parameters, execution_options, *args, **kw)\u001B[0m\n\u001B[0;32m   1841\u001B[0m     \u001B[38;5;28;01mreturn\u001B[39;00m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39m_exec_insertmany_context(dialect, context)\n\u001B[0;32m   1842\u001B[0m \u001B[38;5;28;01melse\u001B[39;00m:\n\u001B[1;32m-> 1843\u001B[0m     \u001B[38;5;28;01mreturn\u001B[39;00m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39m_exec_single_context(\n\u001B[0;32m   1844\u001B[0m         dialect, context, statement, parameters\n\u001B[0;32m   1845\u001B[0m     )\n",
      "File \u001B[1;32mG:\\anaconda\\Lib\\site-packages\\sqlalchemy\\engine\\base.py:1983\u001B[0m, in \u001B[0;36mConnection._exec_single_context\u001B[1;34m(self, dialect, context, statement, parameters)\u001B[0m\n\u001B[0;32m   1980\u001B[0m     result \u001B[38;5;241m=\u001B[39m context\u001B[38;5;241m.\u001B[39m_setup_result_proxy()\n\u001B[0;32m   1982\u001B[0m \u001B[38;5;28;01mexcept\u001B[39;00m \u001B[38;5;167;01mBaseException\u001B[39;00m \u001B[38;5;28;01mas\u001B[39;00m e:\n\u001B[1;32m-> 1983\u001B[0m     \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39m_handle_dbapi_exception(\n\u001B[0;32m   1984\u001B[0m         e, str_statement, effective_parameters, cursor, context\n\u001B[0;32m   1985\u001B[0m     )\n\u001B[0;32m   1987\u001B[0m \u001B[38;5;28;01mreturn\u001B[39;00m result\n",
      "File \u001B[1;32mG:\\anaconda\\Lib\\site-packages\\sqlalchemy\\engine\\base.py:2352\u001B[0m, in \u001B[0;36mConnection._handle_dbapi_exception\u001B[1;34m(self, e, statement, parameters, cursor, context, is_sub_exec)\u001B[0m\n\u001B[0;32m   2350\u001B[0m \u001B[38;5;28;01melif\u001B[39;00m should_wrap:\n\u001B[0;32m   2351\u001B[0m     \u001B[38;5;28;01massert\u001B[39;00m sqlalchemy_exception \u001B[38;5;129;01mis\u001B[39;00m \u001B[38;5;129;01mnot\u001B[39;00m \u001B[38;5;28;01mNone\u001B[39;00m\n\u001B[1;32m-> 2352\u001B[0m     \u001B[38;5;28;01mraise\u001B[39;00m sqlalchemy_exception\u001B[38;5;241m.\u001B[39mwith_traceback(exc_info[\u001B[38;5;241m2\u001B[39m]) \u001B[38;5;28;01mfrom\u001B[39;00m\u001B[38;5;250m \u001B[39m\u001B[38;5;21;01me\u001B[39;00m\n\u001B[0;32m   2353\u001B[0m \u001B[38;5;28;01melse\u001B[39;00m:\n\u001B[0;32m   2354\u001B[0m     \u001B[38;5;28;01massert\u001B[39;00m exc_info[\u001B[38;5;241m1\u001B[39m] \u001B[38;5;129;01mis\u001B[39;00m \u001B[38;5;129;01mnot\u001B[39;00m \u001B[38;5;28;01mNone\u001B[39;00m\n",
      "File \u001B[1;32mG:\\anaconda\\Lib\\site-packages\\sqlalchemy\\engine\\base.py:1964\u001B[0m, in \u001B[0;36mConnection._exec_single_context\u001B[1;34m(self, dialect, context, statement, parameters)\u001B[0m\n\u001B[0;32m   1962\u001B[0m                 \u001B[38;5;28;01mbreak\u001B[39;00m\n\u001B[0;32m   1963\u001B[0m     \u001B[38;5;28;01mif\u001B[39;00m \u001B[38;5;129;01mnot\u001B[39;00m evt_handled:\n\u001B[1;32m-> 1964\u001B[0m         \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mdialect\u001B[38;5;241m.\u001B[39mdo_execute(\n\u001B[0;32m   1965\u001B[0m             cursor, str_statement, effective_parameters, context\n\u001B[0;32m   1966\u001B[0m         )\n\u001B[0;32m   1968\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39m_has_events \u001B[38;5;129;01mor\u001B[39;00m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mengine\u001B[38;5;241m.\u001B[39m_has_events:\n\u001B[0;32m   1969\u001B[0m     \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mdispatch\u001B[38;5;241m.\u001B[39mafter_cursor_execute(\n\u001B[0;32m   1970\u001B[0m         \u001B[38;5;28mself\u001B[39m,\n\u001B[0;32m   1971\u001B[0m         cursor,\n\u001B[1;32m   (...)\u001B[0m\n\u001B[0;32m   1975\u001B[0m         context\u001B[38;5;241m.\u001B[39mexecutemany,\n\u001B[0;32m   1976\u001B[0m     )\n",
      "File \u001B[1;32mG:\\anaconda\\Lib\\site-packages\\sqlalchemy\\engine\\default.py:942\u001B[0m, in \u001B[0;36mDefaultDialect.do_execute\u001B[1;34m(self, cursor, statement, parameters, context)\u001B[0m\n\u001B[0;32m    941\u001B[0m \u001B[38;5;28;01mdef\u001B[39;00m\u001B[38;5;250m \u001B[39m\u001B[38;5;21mdo_execute\u001B[39m(\u001B[38;5;28mself\u001B[39m, cursor, statement, parameters, context\u001B[38;5;241m=\u001B[39m\u001B[38;5;28;01mNone\u001B[39;00m):\n\u001B[1;32m--> 942\u001B[0m     cursor\u001B[38;5;241m.\u001B[39mexecute(statement, parameters)\n",
      "File \u001B[1;32mG:\\anaconda\\Lib\\site-packages\\mysql\\connector\\cursor_cext.py:353\u001B[0m, in \u001B[0;36mCMySQLCursor.execute\u001B[1;34m(self, operation, params, map_results)\u001B[0m\n\u001B[0;32m    345\u001B[0m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39m_executed \u001B[38;5;241m=\u001B[39m (\n\u001B[0;32m    346\u001B[0m     \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39m_stmt_partition[\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124msingle_stmts\u001B[39m\u001B[38;5;124m\"\u001B[39m]\u001B[38;5;241m.\u001B[39mpopleft()\n\u001B[0;32m    347\u001B[0m     \u001B[38;5;28;01mif\u001B[39;00m map_results\n\u001B[0;32m    348\u001B[0m     \u001B[38;5;28;01melse\u001B[39;00m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39m_stmt_partition[\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mmappable_stmt\u001B[39m\u001B[38;5;124m\"\u001B[39m]\n\u001B[0;32m    349\u001B[0m )\n\u001B[0;32m    351\u001B[0m \u001B[38;5;28;01mtry\u001B[39;00m:\n\u001B[0;32m    352\u001B[0m     \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39m_handle_result(\n\u001B[1;32m--> 353\u001B[0m         \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39m_connection\u001B[38;5;241m.\u001B[39mcmd_query(\n\u001B[0;32m    354\u001B[0m             \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39m_stmt_partition[\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mmappable_stmt\u001B[39m\u001B[38;5;124m\"\u001B[39m],\n\u001B[0;32m    355\u001B[0m             raw\u001B[38;5;241m=\u001B[39m\u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39m_raw,\n\u001B[0;32m    356\u001B[0m             buffered\u001B[38;5;241m=\u001B[39m\u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39m_buffered,\n\u001B[0;32m    357\u001B[0m             raw_as_string\u001B[38;5;241m=\u001B[39m\u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39m_raw_as_string,\n\u001B[0;32m    358\u001B[0m         )\n\u001B[0;32m    359\u001B[0m     )\n\u001B[0;32m    360\u001B[0m \u001B[38;5;28;01mexcept\u001B[39;00m MySQLInterfaceError \u001B[38;5;28;01mas\u001B[39;00m err:\n\u001B[0;32m    361\u001B[0m     \u001B[38;5;28;01mif\u001B[39;00m \u001B[38;5;28mhasattr\u001B[39m(err, \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124merrno\u001B[39m\u001B[38;5;124m\"\u001B[39m):\n",
      "File \u001B[1;32mG:\\anaconda\\Lib\\site-packages\\mysql\\connector\\opentelemetry\\context_propagation.py:97\u001B[0m, in \u001B[0;36mwith_context_propagation.<locals>.wrapper\u001B[1;34m(cnx, *args, **kwargs)\u001B[0m\n\u001B[0;32m     95\u001B[0m \u001B[38;5;66;03m# pylint: disable=possibly-used-before-assignment\u001B[39;00m\n\u001B[0;32m     96\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m \u001B[38;5;129;01mnot\u001B[39;00m OTEL_ENABLED \u001B[38;5;129;01mor\u001B[39;00m \u001B[38;5;129;01mnot\u001B[39;00m cnx\u001B[38;5;241m.\u001B[39motel_context_propagation:\n\u001B[1;32m---> 97\u001B[0m     \u001B[38;5;28;01mreturn\u001B[39;00m method(cnx, \u001B[38;5;241m*\u001B[39margs, \u001B[38;5;241m*\u001B[39m\u001B[38;5;241m*\u001B[39mkwargs)\n\u001B[0;32m     99\u001B[0m current_span \u001B[38;5;241m=\u001B[39m trace\u001B[38;5;241m.\u001B[39mget_current_span()\n\u001B[0;32m    100\u001B[0m tp_header \u001B[38;5;241m=\u001B[39m \u001B[38;5;28;01mNone\u001B[39;00m\n",
      "File \u001B[1;32mG:\\anaconda\\Lib\\site-packages\\mysql\\connector\\connection_cext.py:781\u001B[0m, in \u001B[0;36mCMySQLConnection.cmd_query\u001B[1;34m(self, query, raw, buffered, raw_as_string, **kwargs)\u001B[0m\n\u001B[0;32m    779\u001B[0m \u001B[38;5;28;01mexcept\u001B[39;00m MySQLInterfaceError \u001B[38;5;28;01mas\u001B[39;00m err:\n\u001B[0;32m    780\u001B[0m     \u001B[38;5;28;01mif\u001B[39;00m \u001B[38;5;28mhasattr\u001B[39m(err, \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124merrno\u001B[39m\u001B[38;5;124m\"\u001B[39m):\n\u001B[1;32m--> 781\u001B[0m         \u001B[38;5;28;01mraise\u001B[39;00m get_mysql_exception(\n\u001B[0;32m    782\u001B[0m             err\u001B[38;5;241m.\u001B[39merrno, msg\u001B[38;5;241m=\u001B[39merr\u001B[38;5;241m.\u001B[39mmsg, sqlstate\u001B[38;5;241m=\u001B[39merr\u001B[38;5;241m.\u001B[39msqlstate\n\u001B[0;32m    783\u001B[0m         ) \u001B[38;5;28;01mfrom\u001B[39;00m\u001B[38;5;250m \u001B[39m\u001B[38;5;21;01merr\u001B[39;00m\n\u001B[0;32m    784\u001B[0m     \u001B[38;5;28;01mraise\u001B[39;00m InterfaceError(\u001B[38;5;28mstr\u001B[39m(err)) \u001B[38;5;28;01mfrom\u001B[39;00m\u001B[38;5;250m \u001B[39m\u001B[38;5;21;01merr\u001B[39;00m\n\u001B[0;32m    785\u001B[0m \u001B[38;5;28;01mexcept\u001B[39;00m \u001B[38;5;167;01mAttributeError\u001B[39;00m \u001B[38;5;28;01mas\u001B[39;00m err:\n",
      "\u001B[1;31mProgrammingError\u001B[0m: (mysql.connector.errors.ProgrammingError) 1146 (42S02): Table 'students.person_info' doesn't exist\n[SQL: select * from person_info where id>3;]\n(Background on this error at: https://sqlalche.me/e/20/f405)"
     ]
    }
   ],
   "execution_count": 74
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-27T09:35:51.886004500Z",
     "start_time": "2025-10-27T09:26:37.370946Z"
    }
   },
   "cell_type": "code",
   "source": [
    "data = np.arange(12).reshape(3,4)\n",
    "df = pd.DataFrame(data,index=['d','b','c'],columns=['dd','aa','cc','bb'])\n",
    "df"
   ],
   "id": "b9737cd8be73b12f",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "   dd  aa  cc  bb\n",
       "d   0   1   2   3\n",
       "b   4   5   6   7\n",
       "c   8   9  10  11"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>dd</th>\n",
       "      <th>aa</th>\n",
       "      <th>cc</th>\n",
       "      <th>bb</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>d</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>8</td>\n",
       "      <td>9</td>\n",
       "      <td>10</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 44
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-27T09:35:51.914014600Z",
     "start_time": "2025-10-27T09:27:28.287823Z"
    }
   },
   "cell_type": "code",
   "source": "df.sort_index(axis=0)  #行排序",
   "id": "4ec1e4d5d0e5a5f8",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "   dd  aa  cc  bb\n",
       "b   4   5   6   7\n",
       "c   8   9  10  11\n",
       "d   0   1   2   3"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>dd</th>\n",
       "      <th>aa</th>\n",
       "      <th>cc</th>\n",
       "      <th>bb</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>8</td>\n",
       "      <td>9</td>\n",
       "      <td>10</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>d</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 46
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-27T09:35:51.930018600Z",
     "start_time": "2025-10-27T09:27:29.534074Z"
    }
   },
   "cell_type": "code",
   "source": "df.sort_index(axis=1)  #列排序",
   "id": "b79e4567a1936623",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "   aa  bb  cc  dd\n",
       "d   1   3   2   0\n",
       "b   5   7   6   4\n",
       "c   9  11  10   8"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>aa</th>\n",
       "      <th>bb</th>\n",
       "      <th>cc</th>\n",
       "      <th>dd</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>d</th>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>5</td>\n",
       "      <td>7</td>\n",
       "      <td>6</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>9</td>\n",
       "      <td>11</td>\n",
       "      <td>10</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 47
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-27T09:35:51.931013700Z",
     "start_time": "2025-10-27T09:27:31.389152Z"
    }
   },
   "cell_type": "code",
   "source": "df.sort_values(by='cc')  #按’cc’列的数据进行排序",
   "id": "57a50a3b36c8d143",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "   dd  aa  cc  bb\n",
       "d   0   1   2   3\n",
       "b   4   5   6   7\n",
       "c   8   9  10  11"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>dd</th>\n",
       "      <th>aa</th>\n",
       "      <th>cc</th>\n",
       "      <th>bb</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>d</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>8</td>\n",
       "      <td>9</td>\n",
       "      <td>10</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 48
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
